<template>
  <div class="app-container">
    <div class="register-box">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <h3 style="margin-bottom: 20px">注册</h3>
        <el-form-item label="用户名" placeholder="请输入用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" placeholder="请输入密码" prop="password">
          <el-input v-model="form.password" show-password @keyup.enter.native="onRegister"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onRegister">注册</el-button>
        </el-form-item>
      </el-form>
    </div>


  </div>
</template>

<script>

import {register} from "@/api";

export default {
  name: "register",
  data() {
    return {
      form: {
        username: "",
        password: "",
      },
      rules: {
        username: [{required: true, message: "请输入用户名", trigger: ["change", "blur"]},
          {min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur'}],
        password: [{required: true, message: "请输入密码", trigger: ["change", "blur"]}],
      }
    }
  },
  methods: {
    async onRegister() {
      try {
        await this.$refs.form.validate()
        await register(this.form).then(res => {
          // console.log("注册", res)

          const {code, data, msg} = res
          this.$router.push('/')

        })
      } catch (e) {
        console.log("注册", e)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.app-container {
  width: 100vw;
  height: 100vh;
  background-image: linear-gradient(to bottom, #2c3e50, #a6c3e8);
  display: flex;
  justify-content: center;
  align-items: center;

  .el-form {
    .el-form-item {
      ::v-deep .el-form-item__label {
        color: #fff;
      }
    }
  }
}

</style>
